	<?php $this->load->view('admin/partials/tournament_menu'); ?>
	<?php 
		$ci= &get_instance();
		$live_tournament = $ci->tournament_entity->getTournamentByStatus("living");
		$tournament_status_array = tournamentStatusDataConfig();
	?>
	
	<div class="clrboth"></div>
	
	<h3>Tournament &raquo; <?php echo ucfirst($this->uri->segment(4,''));?></h3>
	<?php $buttons = array('buttons'=>array('Add#add_new_tournament();') );?>
	<?php $this->load->view('admin/partials/buttons', $buttons ); ?>
	
	<div class="clrboth"></div>
	
	<table border="0" class="table-list">
		<thead>
			<tr>
			<!--	<th>Sport</th>	-->
				<th>Tournament</th>
				<th>Start</th>
				<th>End</th>
				<th>Prize</th>
				<th>Rank</th>
				<th>Event</th>
				<th>Event Manual</th>
				<th>Status</th>
				<th><?php echo lang('dataman.manage'); ?></th>
			</tr>
		</thead>
		
		<tbody>
			<?php foreach( $live_tournament as $item ): ?>
				<?php 
					$TOURNAMENT = $ci->tournament_entity->init('id',$item->id);
					$SPORT = $ci->sport_entity->init('id', $TOURNAMENT->getSportId());
				?>
				<?php if($TOURNAMENT->getPublish() == 'publish'):?>
					<tr>
					 
						<td><a href="<?php echo site_url('admin/dataman/match/in_tournament/?t_id='.$TOURNAMENT->getId());?>" ><?php echo $TOURNAMENT->getName(); ?></a></td>
						<td><?php echo sysDateTimeFormat($TOURNAMENT->getStartTime(),DATE_FM); ?></td>
						<td><?php echo sysDateTimeFormat($TOURNAMENT->getEndTime(),DATE_FM); ?></td>
						<td>
							<a href="javascript:void(0);" onclick="prize_add(<?php echo $TOURNAMENT->getId();?>)">Add</a>&nbsp;|&nbsp;
							<a href="javascript:void(0);" onclick="prize_view(<?php echo $TOURNAMENT->getId();?>)">View</a>
							<br/>
							<a href="<?php echo site_url('admin/dataman/weekly_prize/list/?t_id='.$TOURNAMENT->getId());?>">Weekly Prize</a>
						</td>
						<td><a href="javascript:void(0);" onclick="rank_view(<?php echo $TOURNAMENT->getId();?>)">View</a></td>
						<td><a href="javascript:void(0);" onclick="open_event_view(<?php echo $TOURNAMENT->getId();?>)">View</a></td>
						<td><a href="javascript:void(0);" onclick="view_manual_event(<?php echo $TOURNAMENT->getId();?>)">View</a></td>
						<td>
							<?php echo $tournament_status_array[$TOURNAMENT->getStatus()];?>
						</td>
						<td>
							<a href="javascript:void(0);" onclick="tournament_edit(<?php echo $TOURNAMENT->getId();?>,'live')"><?php echo lang('dataman.edit');?></a>
							&nbsp;|&nbsp;
							<a href="javascript:void(0);" onclick="tournament_cancel(<?php echo $TOURNAMENT->getId();?>)" >Cancel</a>
							&nbsp;|&nbsp;
							<a href="javascript:void(0);" onclick="tournament_publish_unpublish(<?php echo $TOURNAMENT->getId();?>)" ><?php echo ucfirst($TOURNAMENT->getPublish());?></a>
							<?php if($TOURNAMENT->getIsFinish() == 'no'):?>
								&nbsp;|&nbsp;
								<a href="javascript:void(0);" onclick="tournament_finish(<?php echo $TOURNAMENT->getId();?>)" >Finish</a>
							<?php endif;?>
						</td>
					</tr>
				<?php endif; ?>	
			<?php endforeach; ?>
		</tbody>
	</table>
	
	<script type="text/javascript">
		function open_event_view(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/view_event',{tournament_id:tournament_id},function(res){
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
					{
						 width: 1020,
						 resizable: false,
						 height:550,
						 title: 'View Event: Live/UpComing' ,
						 zIndex: 1005
					}
				);
			});
		}
		
		function view_event_closed(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/view_event_closed',{tournament_id:tournament_id},function(res){
				jQuery('#dialog').html(res);
				jQuery('#dialog').dialog(
					{
						 width: 1020,
						 resizable: false,
						 height:550,
						 title: 'View Event: Closed' 
					}
				);
			});
		}
				
		function view_event_cancelled(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/view_event_cancelled',{tournament_id:tournament_id},function(res){
				jQuery('#dialog').html(res);
				jQuery('#dialog').dialog(
					{
						 width: 1020,
						 resizable: false,
						 height:550,
						 title: 'View Event: Cancelled' 
					}
				);
			});
		}
		
		function view_event_hidden_category(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/view_event_hidden_category',{tournament_id:tournament_id},function(res){
				jQuery('#dialog').html(res);
				jQuery('#dialog').dialog(
					{
						 width: 1020,
						 resizable: false,
						 height:550,
						 title: 'View Event: Hidden categories' 
					}
				);
			});
		}
		
		function add_category_event(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/add_category_event',{tournament_id:tournament_id},function(res){
				jQuery('#dialog3').html(res);
				jQuery('#dialog3').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:200,
						 title: 'Add Category Event' 
					}
				);
			});
		}
		
		function add_event(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/add_event',{tournament_id:tournament_id},function(res){
				jQuery('#dialog3').html(res);
				jQuery('#dialog3').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:400,
						 title: 'Add Event' 
					}
				);
			});
		}
		
		function event_category_open_hide(category_id,tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_category_open_hide',{category_id:category_id,tournament_id:tournament_id},function(res){
				if(res== 'ok'){
					$mode = jQuery('#dialog_mode').val();
					if($mode == 'open'){
						open_event_view(tournament_id);
					}else{
						view_event_hidden_category(tournament_id);
					}
				}else{
					alert(res);
				}
			});
		}
		
		function event_category_edit(event_category_id, tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_category_edit',{id:event_category_id,tournament_id:tournament_id},function(res){
				jQuery('#dialog2').html(res);
				jQuery('#dialog2').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:200,
						 title: 'Edit Category Event' 
					}
				);
			});
		}
		
		function event_change_bet_status(event_id, tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_change_bet_status',{id:event_id},function(res){
				if(res== 'ok'){
					$mode = jQuery('#dialog_mode').val();
					if($mode == 'open'){
						open_event_view(tournament_id);
					}else{
						view_event_hidden_category(tournament_id);
					}
				}else{
					alert(res);
				}
			});
		}
		
		function set_event_result(event_id,result){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/set_event_result',{id:event_id,result:result},function(res){
			});
		}
		
		function event_edit(event_id, tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/edit_event',{tournament_id:tournament_id,id:event_id},function(res){
				jQuery('#dialog2').html(res);
				jQuery('#dialog2').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:400,
						 title: 'Edit Event' 
					}
				);
			});
		}
		
		function openOrderingDialog(tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/ordering_category',{tournament_id:tournament_id},function(res){
				jQuery('#dialog3').html(res);
				jQuery('#dialog3').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:400,
						 title: 'Ordering Category' ,
						 zIndex: 1015
					}
				);
				jQuery('#sortable').sortable();
			});
		}
		
		function add_events_to_category(category_id, tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/add_events_to_category',{tournament_id:tournament_id,category_id:category_id},function(res){
				jQuery('#dialog2').html(res);
				jQuery('#dialog2').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:600,
						 title: 'Add Events To This Category' 
					}
				);
			});
		}
		
		function add_more_event(category_id,tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/add_more_event',{category_id:category_id,tournament_id:tournament_id},function(res){
				jQuery('#addMore').append(res);
			});
		}
				
		function remove_event(uid){
			jQuery('#wrap_'+uid).remove();
		}
		
		function edit_all_events_of_category(category_id,tournament_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/edit_all_events_of_category',{tournament_id:tournament_id,category_id:category_id},function(res){
				jQuery('#dialog2').html(res);
				jQuery('#dialog2').dialog(
					{
						 width: 650,
						 resizable: false,
						 height:600,
						 title: 'Edit Events Of This Category' 
					}
				);
			});
		}
		
		function who_bet_event(event_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/who_bet_event',{id:event_id},function(res){
				jQuery('#dialog2').html(res);
				jQuery('#dialog2').dialog(
					{
						 width: 700,
						 resizable: false,
						 height:400,
						 title: 'User bet event' 
					}
				);
			});
		}
		
		function event_cancel(event_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_cancel',{event_id:event_id},function(res){
				var p = res.split('#');
				if(p[0]== 'ok'){
					var tournament_id = p[1];
					$mode = jQuery('#dialog_mode').val();
					if($mode == 'open'){
						open_event_view(tournament_id);
					}else{
						view_event_hidden_category(tournament_id);
					}
				}else{
					alert(res);
				}
			});
		}
		
		function event_finish(event_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_finish',{event_id:event_id},function(res){
				var p = res.split('#');
				if(p[0]== 'ok'){
					var tournament_id = p[1];
					$mode = jQuery('#dialog_mode').val();
					if($mode == 'open'){
						open_event_view(tournament_id);
					}else{
						view_event_hidden_category(tournament_id);
					}
				}else{
					alert(res);
				}
			});
		}
		
		function event_category_cancel(event_category_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_category_cancel',{event_category_id:event_category_id},function(res){
				var p = res.split('#');
				if(p[0]== 'ok'){
					var tournament_id = p[1];
					$mode = jQuery('#dialog_mode').val();
					if($mode == 'open'){
						open_event_view(tournament_id);
					}else{
						view_event_hidden_category(tournament_id);
					}
				}else{
					alert(res);
				}
			});
		}
		
		function event_category_finish(event_category_id){
			jQuery.post(BASE_URI+'admin/dataman/tournament_event/event_category_finish',{event_category_id:event_category_id},function(res){
				var p = res.split('#');
				if(p[0]== 'ok'){
					var tournament_id = p[1];
					$mode = jQuery('#dialog_mode').val();
					if($mode == 'open'){
						open_event_view(tournament_id);
					}else{
						view_event_hidden_category(tournament_id);
					}
				}else{
					alert(res);
				}
			});
		}
		
		jQuery(document).ready(function(){
			var optionsEventCategory = {
				beforeSubmit:beforeSubmitEventCategory,
				success:afterSubmitEventCategory
			};
			var optionsEvent = { 
				beforeSubmit:beforeSubmitEvent,   
				success:afterSubmitEvent
			};	
			var optionsEvent2 = { 
				beforeSubmit:function(formData, jqForm, options){return true;},   
				success:function(responseText, statusText, xhr, $form){
					if(isNaturalNumber(responseText)){
						//jQuery('#dialog').dialog('close');
						jQuery('#dialog2').dialog('close');
						jQuery('#dialog3').dialog('close');
						 
						$mode = jQuery('#dialog_mode').val();
						if($mode == 'open'){
							open_event_view(responseText);
						}else{
							view_event_hidden_category(responseText);
						}
					}else{
						alert(responseText);
					}
				}
			};	
			
			jQuery("#event_category_submit").live("submit",function(e){
				e.preventDefault(); // <-- important
				jQuery(this).ajaxSubmit(optionsEventCategory);
				return false;
			});	
			jQuery("#event_submit").live("submit",function(e){
				e.preventDefault(); // <-- important
				jQuery(this).ajaxSubmit(optionsEvent);
				return false;
			});	
			jQuery("#add_events_submit").live("submit",function(e){
				e.preventDefault(); // <-- important
				jQuery(this).ajaxSubmit(optionsEvent2);
				return false;
			});	
			jQuery("#edit_events_submit").live("submit",function(e){
				e.preventDefault(); // <-- important
				jQuery(this).ajaxSubmit(optionsEvent2);
				return false;
			});	
			
			jQuery('#button_save_ordering').live('click',function(){
				jQuery('#loader_image_ordering').show();
				$tournament_id = jQuery('#match_id_ordering_form').val();
				$idArray = [];
				jQuery('#sortable .ui-state-default').each(function(i,o){
					$idArray.push(jQuery(this).attr('id'));
				});
				$ids = $idArray ? $idArray.join(','):'';
				jQuery.post(BASE_URI+'admin/dataman/tournament_event/update_ordering_category',{ids:$ids,tournament_id:$tournament_id},function(res){
					if(res == 'ok'){
					 
						$mode = jQuery('#dialog_mode').val();
						if($mode == 'open'){
							open_event_view($tournament_id);
						}else{
							view_event_hidden_category($tournament_id);
						}
						jQuery('#dialog3').dialog( "close" );
					}else{
						alert(res);
					}
				});
			});
		});
		
				
		function beforeSubmitEvent(formData, jqForm, options){
			jQuery('#loader').show();
			
			json = {};
			for( e in formData){
				json[formData[e].name] = formData[e].value;
			}
			
			var arrayRequire = [ 'name', 'odds' ]; 
			var err = 0;
			for(var i=0 ;i< arrayRequire.length; i++ ){
				if( isEmpty(json[arrayRequire[i]]) ){
					jQuery('#'+arrayRequire[i]+'_error').show();
					err++;
				}else{
					jQuery('#'+arrayRequire[i]+'_error').hide();
				}
			}
			
			if(err > 0){	
				jQuery('#loader').hide();
				alert("Error. Any fields are missing.");
				return false;
			}else{
				return true;
			}
		}

		function afterSubmitEvent(responseText, statusText, xhr, $form){
			jQuery('#loader').hide();	
			if(isNaturalNumber(responseText)){
				//jQuery('#dialog').dialog('close');
				jQuery('#dialog2').dialog('close');
				jQuery('#dialog3').dialog('close');
				 
				$mode = jQuery('#dialog_mode').val();
				if($mode == 'open'){
					open_event_view(responseText);
				}else{
					view_event_hidden_category(responseText);
				}
			}else{
				alert(responseText);
			}
		}

		function beforeSubmitEventCategory(formData, jqForm, options){
			jQuery('#loader').show();
			json = {};
			for( e in formData){
				json[formData[e].name] = formData[e].value;
			}
			
			var arrayRequire = [ 'category' ]; 
			var err = 0;
			for(var i=0 ;i< arrayRequire.length; i++ ){
				if( isEmpty(json[arrayRequire[i]]) ){
					jQuery('#'+arrayRequire[i]+'_error').show();
					err++;
				}else{
					jQuery('#'+arrayRequire[i]+'_error').hide();
				}
			}
			
			if(err > 0){	
				jQuery('#loader').hide();
				alert("Error. Any fields are missing.");
				return false;
			}else{
				return true;
			}
		} 
		 
		function afterSubmitEventCategory(responseText, statusText, xhr, $form){
			jQuery('#loader').hide();	
			 
			if(isNaturalNumber(responseText)){
				//jQuery('#dialog').dialog('close');
				jQuery('#dialog2').dialog('close');
				jQuery('#dialog3').dialog('close');
				
				$mode = jQuery('#dialog_mode').val();
				if($mode == 'open'){
					open_event_view(responseText);
				}else{
					view_event_hidden_category(responseText);
				}
			}else{
				alert(responseText);
			}
		} 
		
		///////////////////////////////////////////////////////////////////////////////////////MANUAL EVENT/////////////////////////////
		
		
		
		
	</script>
	
<?php $this->load->view( 'admin/tournament/add_edit_tournament' ); ?>